package stackdemo;

import org.w3c.dom.Node;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-01-23
 * Time: 11:02
 */
public class MyStack {
    private int[] elem;
    private int usedSize;
    private static final int DEFAULT_SIZE = 10;

    public MyStack(){
        elem = new int[10];
    }

    public void push (int val){
        if (isFull()){
            //扩容
            grow();
        }
        elem[usedSize] = val;
        usedSize++;
    }
    public void grow(){
        elem = Arrays.copyOf(elem,elem.length);
    }

    public boolean isFull(){
        return usedSize == elem.length;
    }

    public int pop(){
        if (isEmpty()) {
            throw new StackEmptyException("栈空异常");
        }
        usedSize--;

        return elem[usedSize];
    }
    public int peek(){
        if (isEmpty()) {
            throw new StackEmptyException("栈空异常");
        }

        return elem[usedSize-1];
    }
    public boolean isEmpty(){
        return usedSize == 0;
    }


}
